package com.rabbitmq.delay.producer;

import com.rabbitmq.delay.config.RabbitLazyConfigure;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.support.converter.AbstractJavaTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: 胡成
 * @Version: 0.0.1V
 * @Date: 2018/10/25
 * @Description: 延迟发送消息
 **/
@Component
public class LazySender {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void sendlazy(Object msg){
        rabbitTemplate.convertAndSend(RabbitLazyConfigure.REGISTER_DELAY_EXCHANGE, RabbitLazyConfigure.DELAY_ROUTING_KEY, msg, message -> {
            message.getMessageProperties().setHeader(AbstractJavaTypeMapper.DEFAULT_CONTENT_CLASSID_FIELD_NAME, msg);
            //如果配置了 params.put("x-message-ttl", 5 * 1000);
            // 那么这一句也可以省略,具体根据业务需要是声明 Queue 的时候就指定好延迟时间还是在发送自己控制时间
            message.getMessageProperties().setExpiration(5 * 1000 + "");
            return message;
        });
    }

}
